/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ExcelAccess;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FontUnderline;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author User
 */
public class ExcelExporter {
    public ArrayList<ArrayList<String>> objectContents = null;
    public ArrayList<ArrayList<String>> serviceCallsContents = null;
    
    public ExcelExporter(ArrayList<ArrayList<String>> objectContents,
            ArrayList<ArrayList<String>> serviceCallsContents){
        this.objectContents = objectContents;
        this.serviceCallsContents = serviceCallsContents;
    }
    
    public void writeContents(File f) throws Exception{
        FileOutputStream fileOutputStream = new FileOutputStream(f);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("Export Report");
        
        if(this.objectContents != null)
            for(int i = 0; i < ExcelFormat.objectsFields.length; i++) sheet.setColumnWidth(i, 8000);
        else if(this.serviceCallsContents != null)
            for(int i = 0; i < ExcelFormat.serviceCallFields.length; i++) sheet.setColumnWidth(i, 8000);
        
        int row = 0;
        XSSFRow rowData = null;
        // create header for this file
        rowData = sheet.createRow(row);
        XSSFCell bigHeader = rowData.createCell(0);
        bigHeader.setCellStyle(this.getCellStyle(xssfWorkbook, 20, "Calibri", true, FontUnderline.SINGLE,false));
        bigHeader.setCellValue("Exported Huisman Service Reports");
        row++;
        
        rowData = sheet.createRow(row);
        row++;
        
        if(this.objectContents != null){
            for(int i = 0; i < ExcelFormat.objectsFields.length; i++){
                XSSFCell cell = rowData.createCell(i);
                cell.setCellStyle(this.getCellStyle(xssfWorkbook, 14, "Calibri", true, FontUnderline.NONE,true));
                cell.setCellValue(ExcelFormat.objectsFields[i]);
            }
            for(int i = 0; i < objectContents.size(); i++){
                rowData = sheet.createRow(row);
                row++;
                for(int j = 0; j < ExcelFormat.objectsFields.length; j++){
                    XSSFCell cell = rowData.createCell(j);
                    cell.setCellStyle(this.getCellStyle(xssfWorkbook, 14, "Calibri", false, FontUnderline.NONE,true));
                    cell.setCellValue(this.objectContents.get(i).get(j));
                }
            }
        } else if(this.serviceCallsContents != null){
            for(int i = 0; i < ExcelFormat.serviceCallFields.length; i++){
                XSSFCell cell = rowData.createCell(i);
                cell.setCellStyle(this.getCellStyle(xssfWorkbook, 14, "Calibri", true, FontUnderline.NONE,true));
                cell.setCellValue(ExcelFormat.serviceCallFields[i]);
            }
            for(int i = 0; i < serviceCallsContents.size(); i++){
                rowData = sheet.createRow(row);
                row++;
                for(int j = 0; j < ExcelFormat.serviceCallFields.length; j++){
                    XSSFCell cell = rowData.createCell(j);
                    cell.setCellStyle(this.getCellStyle(xssfWorkbook, 14, "Calibri", false, FontUnderline.NONE,true));
                    cell.setCellValue(this.serviceCallsContents.get(i).get(j));
                }
            }
        }
        xssfWorkbook.write(fileOutputStream);
    }
    
    private XSSFCellStyle getCellStyle(XSSFWorkbook xssfWorkbook,int fontSize,
            String fontName,boolean ifBold,FontUnderline fontUnderLine,boolean border){
         XSSFCellStyle style = xssfWorkbook.createCellStyle();
         XSSFFont objectFont = xssfWorkbook.createFont();
         objectFont.setFontHeight(fontSize);
         objectFont.setFontName(fontName);
         objectFont.setBold(ifBold);
         objectFont.setUnderline(fontUnderLine);
         style.setFont(objectFont);
         style.setWrapText(true);
         style.setVerticalAlignment(VerticalAlignment.TOP);
         if(border){
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
         }
         return style;
    }
}
